package cn.edu.klmy.controller;

import cn.edu.klmy.dao.IClassroomDao;
import cn.edu.klmy.models.classroom.Classroom;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import web.models.CommonResult;
import web.models.Page;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/classroom")
public class ClassroomController {

    @Resource(name = "classroomDao")
    private IClassroomDao classroomDao;

    @RequestMapping("/add")
    @ResponseBody
    public CommonResult addClassroom(@ModelAttribute Classroom classroom) {
        classroomDao.addClassroom(classroom);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/update")
    @ResponseBody
    public CommonResult updateClassroom(@ModelAttribute Classroom classroom) {
        classroomDao.updateClassroom(classroom);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/remove")
    @ResponseBody
    public CommonResult removeClassroom(@RequestParam int classroomId) {
        classroomDao.removeClassroom(classroomId);
        return new CommonResult(new Boolean(true));
    }

    @RequestMapping("/{classroomId}")
    @ResponseBody
    public Classroom getClassroom(@PathVariable int classroomId) {
        return classroomDao.getClassroom(classroomId);
    }

    @RequestMapping("/exists")
    @ResponseBody
    public CommonResult classroomExists(@RequestParam String classroomLocation) {
        boolean result =  classroomDao.classroomExists(classroomLocation);
    	return new CommonResult(new Boolean(result));
    }

    @RequestMapping("/all")
    @ResponseBody
    public Page<Classroom> getClassroomsPage(@RequestParam int page, @RequestParam int rows) {
        List<Classroom> classrooms = classroomDao.getClassrooms(null, page, rows);
        int count = classroomDao.getClassroomCount(null);
        return new Page<Classroom>(count, classrooms);
    }

    @RequestMapping("/list/all")
    @ResponseBody
    public List<Classroom> getAllClassroom(){
        return classroomDao.getAllClassroom();
    }

    public void setClassroomDao(IClassroomDao classroomDao) {
        this.classroomDao = classroomDao;
    }
}
